package cn.csc.module.order.controller.admin.logistics;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.order.controller.admin.logistics.vo.*;
import cn.csc.module.order.convert.logistics.OrderLogisticsConvert;
import cn.csc.module.order.dal.dataobject.logistics.OrderLogisticsDO;
import cn.csc.module.order.service.logistics.OrderLogisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 订单物流")
@RestController
@RequestMapping("/order/logistics")
@Validated
public class OrderLogisticsController {

    @Resource
    private OrderLogisticsService logisticsService;

    @PostMapping("/create")
    @ApiOperation("创建订单物流")
    @PreAuthorize("@ss.hasPermission('order:logistics:create')")
    public CommonResult<BigInteger> createLogistics(@Valid @RequestBody OrderLogisticsCreateReqVO createReqVO) {
        return success(logisticsService.createLogistics(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新订单物流")
    @PreAuthorize("@ss.hasPermission('order:logistics:update')")
    public CommonResult<Boolean> updateLogistics(@Valid @RequestBody OrderLogisticsUpdateReqVO updateReqVO) {
        logisticsService.updateLogistics(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除订单物流")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('order:logistics:delete')")
    public CommonResult<Boolean> deleteLogistics(@RequestParam("id") BigInteger id) {
        logisticsService.deleteLogistics(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得订单物流")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('order:logistics:query')")
    public CommonResult<OrderLogisticsRespVO> getLogistics(@RequestParam("id") BigInteger id) {
        OrderLogisticsDO logistics = logisticsService.getLogistics(id);
        return success(OrderLogisticsConvert.INSTANCE.convert(logistics));
    }

    @GetMapping("/list")
    @ApiOperation("获得订单物流列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('order:logistics:query')")
    public CommonResult<List<OrderLogisticsRespVO>> getLogisticsList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<OrderLogisticsDO> list = logisticsService.getLogisticsList(ids);
        return success(OrderLogisticsConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得订单物流分页")
    @PreAuthorize("@ss.hasPermission('order:logistics:query')")
    public CommonResult<PageResult<OrderLogisticsRespVO>> getLogisticsPage(@Valid OrderLogisticsPageReqVO pageVO) {
        PageResult<OrderLogisticsDO> pageResult = logisticsService.getLogisticsPage(pageVO);
        return success(OrderLogisticsConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出订单物流 Excel")
    @PreAuthorize("@ss.hasPermission('order:logistics:export')")
    @OperateLog(type = EXPORT)
    public void exportLogisticsExcel(@Valid OrderLogisticsExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<OrderLogisticsDO> list = logisticsService.getLogisticsList(exportReqVO);
        // 导出 Excel
        List<OrderLogisticsExcelVO> datas = OrderLogisticsConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "订单物流.xls", "数据", OrderLogisticsExcelVO.class, datas);
    }

}
